package com.atgm.mall.gmmailware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyRabbitMqConfig {

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 容器中的Binding、Queue Exchange  自动创建
     * @return
     */
    @Bean
    public Queue wareDelayQueue() {
        Map<String, Object> param = new HashMap<>();
        param.put("x-dead-letter-exchange", "pay-order-event-exchange");
        param.put("x-dead-letter-routing-key", "pay.order.release");
        param.put("x-message-ttl", 120000);
        Queue queue = new Queue("pay.order.delay.queue", true, false, false, param);
        return queue;
    }

    @Bean
    public Queue wareReleaseStockQueue() {
        return new Queue("pay.order.release.stock.queue", true, false, false);
    }

    @Bean
    public Exchange wareEventExchange() {
        return new TopicExchange("pay-order-event-exchange", true, false);
    }

    @Bean
    public Binding wareDelayBinding() {
        return new Binding("pay.order.delay.queue", Binding.DestinationType.QUEUE, "pay-order-event-exchange", "pay.order.locked", null);
    }

    @Bean
    public Binding wareLockedBinding() {
        return new Binding("pay.order.release.stock.queue", Binding.DestinationType.QUEUE, "pay-order-event-exchange",
                "pay.order.release.#", null);
    }

}
